package com.ruoyi.teaching.service.impl;

import java.time.LocalDate;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.teaching.bo.HolidayConfigBo;
import com.ruoyi.teaching.domain.YaHoliday;
import com.ruoyi.teaching.mapper.YaHolidayMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaHolidayConfigMapper;
import com.ruoyi.teaching.domain.YaHolidayConfig;
import com.ruoyi.teaching.service.IYaHolidayConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 节假日配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService
{
    /**
     * 国家法定节假日数量
     */
    public static final int HOLIDAY_SIZE = 7;
    @Autowired
    private YaHolidayConfigMapper yaHolidayConfigMapper;
    @Autowired
    private YaHolidayMapper yaHolidayMapper;
    /**
     * 查询节假日配置
     *
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public YaHolidayConfig selectYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     *
     * @param yaHolidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
    }

    /**
     * 新增节假日配置
     *
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int insertYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 修改节假日配置
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateYaHolidayConfig(List<HolidayConfigBo> holidayConfigBoList)
    {
        if (CollectionUtils.isEmpty(holidayConfigBoList) || holidayConfigBoList.size() != HOLIDAY_SIZE) {
            return 0;
        }
        try {
            //1.判断该年份配置是否存在,存在先删除（先删多方，再删一方）
            HolidayConfigBo holidayConfigBo = holidayConfigBoList.get(1);
            String year = holidayConfigBo.getDate().substring(0,4);
            yaHolidayMapper.deleteByYear(year);
            yaHolidayConfigMapper.deleteByYear(year);
            //2.新增配置表（1方）
            YaHolidayConfig yaHolidayConfig = new YaHolidayConfig();
            yaHolidayConfig.setYear(year);

            ObjectMapper objectMapper = new ObjectMapper();
            yaHolidayConfig.setContent(objectMapper.writeValueAsString(holidayConfigBoList));

            yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);
            Long configId = yaHolidayConfig.getId();
            //3.新增节假日表（多方）(维护外键-和1方的关系)
            holidayConfigBoList.forEach(config->{
                String description = config.getDescription();
                Integer count = config.getCount();
                LocalDate beginDate = LocalDate.parse(config.getDate());
                for (int i = 0; i < count; i++) {
                    YaHoliday yaHoliday = new YaHoliday();
                    yaHoliday.setConfigId(configId);
                    yaHoliday.setDate(beginDate.toString());
                    yaHoliday.setDescription(description);
                    yaHoliday.setWeekday(beginDate.getDayOfWeek().getValue());

                    yaHolidayMapper.insertYaHoliday(yaHoliday);

                    //日期递增
                    beginDate = beginDate.plusDays(1);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 1;
    }

    /**
     * 批量删除节假日配置
     *
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigByIds(Long[] ids)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
    }

    /**
     * 删除节假日配置信息
     *
     * @param id 节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigById(id);
    }
}
